<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/26 0026
 * Time: 上午 11:13
 */
namespace app\api\controller;

use app\common\model\Storelabel;
use app\common\model\Storelabelconnect;
use app\common\model\Userfocus;
use app\common\model\Userreceivetic;
use think\Request;

class User extends Base{
    //编辑用户信息
    public function editUser(Request $request){
        $param = $request->param();
        $User = \app\common\model\User::where('openId',$param['user']['openId'])->find();
        if($User){
            $User->userName = $param['user']['userName'];
            $User->imgUrl = $param['user']['imgUrl'];
            $User->sex = $param['user']['sex'];
            $User->save();

            $Member = \app\common\model\Member::get($User->id);
            if($Member && empty($Member->name)){
                $Member->name = $param['user']['userName'];
                $Member->save();
            }

            return show(config('status.SUCCESS_STATUS'),'ok',1);

        }
    }

        //我的优惠券
    public function myTicket(Request $request){
        $param = $request->param();
        if($param['status']!=0 && $param['status']!=1 && $param['status']!=2){
            return show(config('status.ERROR_STATUS'),'参数错误','参数错误');
        }
        $start = $param['start'];
        $length = $param['length'];
        $Userreceivetic = Userreceivetic::where(['userId'=>$param['userId'],'status'=>$param['status']])->limit($start,$length)->select();
        $count = count(Userreceivetic::where(['userId'=>$param['userId'],'status'=>$param['status']])->select());
        if($Userreceivetic){
            $data['tickets'] = [];
            $data['totalSize'] = $count;
            foreach ($Userreceivetic as $v){
                $tickets = \app\admin\model\Ticket::get($v['ticketId']);
                $Store = \app\admin\model\Store::get($v['storeId']);
                $tickets['store'] = $Store;
                array_push($data['tickets'],$tickets);
            }
            return show(config('status.SUCCESS_STATUS'),'ok',$data);
        }else{
            return show(2,'暂未领取优惠券',NULL);
        }

    }

    //我的关注
    public function myFocus(Request $request){
        $param = $request->param();
        $join = [
            ['__STORE__ s', 'u.storeId = s.id']
        ];
        $Userfocus = model('userfocus')->alias('u')->where('u.userId',$param['userId'])->where('s.status',1)->order('u.focusTime DESC')->join($join)->select();
        if($Userfocus){
            $data = [];
            foreach ($Userfocus as &$v){
                $Storelabelconnect = Storelabelconnect::where('storeId',$v['storeId'])->select();
                if($Storelabelconnect){
                    $lab = [];
                    foreach ($Storelabelconnect as $value){
                        $Storelabel = Storelabel::get($value['storeLabelId']);
                        array_push($lab,$Storelabel);
                    }
                    $v['storeLabels'] = $lab;
                }
                array_push($data,$v);
            }

            return show(config('status.SUCCESS_STATUS'),'ok',$data);
        }else{
            return show(2,'暂未关注店铺',NULL);
        }
    }
}